package com.example.team.service.Imp;

import com.example.team.dao.ISafetyEquipmentPurchaseDetailsDao;
import com.example.team.entity.SafetyEquipmentPurchaseDetails;
import com.example.team.service.ISafetyEquipmentPurchaseDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 编写者：
 * 时间：
 */
//服务接口规范的方法实现
@Service
public class ISafetyEquipmentPurchaseDetailsServiceImpl implements ISafetyEquipmentPurchaseDetailsService {

    @Autowired
    private ISafetyEquipmentPurchaseDetailsDao safetyEquipmentPurchaseDetailsMapper;
    @Override
    public int insertSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        //设置成功状态
        int safetyEquipmentPurchaseDetailsValue;
        SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails1=new SafetyEquipmentPurchaseDetails();
        safetyEquipmentPurchaseDetails1.setDetailsId(safetyEquipmentPurchaseDetails.getDetailsId());
        SafetyEquipmentPurchaseDetails allSafetyEquipmentPurchaseDetails=safetyEquipmentPurchaseDetailsMapper.getSafetyEquipmentPurchaseDetailsById(safetyEquipmentPurchaseDetails1);
        if(allSafetyEquipmentPurchaseDetails!=null){
            safetyEquipmentPurchaseDetailsValue = -1;
        }else {
            safetyEquipmentPurchaseDetailsValue= safetyEquipmentPurchaseDetailsMapper.insertSafetyEquipmentPurchaseDetails(safetyEquipmentPurchaseDetails);
        }
        //返回状态码
        return safetyEquipmentPurchaseDetailsValue;
    }

    @Override
    public int deleteSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        //设置成功状态
        int safetyEquipmentPurchaseDetailsValue;
        SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails1=new SafetyEquipmentPurchaseDetails();
        safetyEquipmentPurchaseDetails1.setApplicant(safetyEquipmentPurchaseDetails.getApplicant());
        List<SafetyEquipmentPurchaseDetails> allSafetyEquipmentPurchaseDetails=safetyEquipmentPurchaseDetailsMapper.getAllSafetyEquipmentPurchaseDetails(safetyEquipmentPurchaseDetails1);
        if(allSafetyEquipmentPurchaseDetails.size()==0){
            safetyEquipmentPurchaseDetailsValue = -1;
        }else {
            safetyEquipmentPurchaseDetailsValue= safetyEquipmentPurchaseDetailsMapper.deleteSafetyEquipmentPurchaseDetails(safetyEquipmentPurchaseDetails);
        }
        //返回状态码
        return safetyEquipmentPurchaseDetailsValue;
    }

    @Override
    public int updateSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        //设置成功状态
        int safetyEquipmentPurchaseDetailsValue;
        //通过主键id对数据更新，判断是否存在目标id
        SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails1=new SafetyEquipmentPurchaseDetails();
        safetyEquipmentPurchaseDetails1.setDetailsId(safetyEquipmentPurchaseDetails.getDetailsId());
        SafetyEquipmentPurchaseDetails allSafetyEquipmentPurchaseDetails=safetyEquipmentPurchaseDetailsMapper.getSafetyEquipmentPurchaseDetailsById(safetyEquipmentPurchaseDetails1);
        if(allSafetyEquipmentPurchaseDetails==null){
            //如果为空，说明无法找到匹配的目标Id，设置状态码-1
            safetyEquipmentPurchaseDetailsValue = -1;
        }else {
            //否则执行更新数据库功能，同时赋值更新成功的数据数量到状态码
            safetyEquipmentPurchaseDetailsValue= safetyEquipmentPurchaseDetailsMapper.updateSafetyEquipmentPurchaseDetails(safetyEquipmentPurchaseDetails);
        }
        //返回状态码
        return safetyEquipmentPurchaseDetailsValue;
    }

    @Override
    public List<SafetyEquipmentPurchaseDetails> getAllSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        return safetyEquipmentPurchaseDetailsMapper.getAllSafetyEquipmentPurchaseDetails(safetyEquipmentPurchaseDetails);
    }

    @Override
    public SafetyEquipmentPurchaseDetails getSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        return safetyEquipmentPurchaseDetailsMapper.getSafetyEquipmentPurchaseDetails(safetyEquipmentPurchaseDetails);
    }
}
